package br.com.mynage.service.impl;

import java.util.ArrayList;
import java.util.List;

import br.com.mynage.dao.ITarefaDAO;
import br.com.mynage.dao.impl.TarefaDaoImpl;
import br.com.mynage.dao.impl.UsuarioDaoImpl;
import br.com.mynage.dominio.Sprint;
import br.com.mynage.dominio.Tarefa;
import br.com.mynage.service.IServiceTarefa;

/**
 * Servi�o(implementa��o) 
 * @author Elaine
 *
 */
public class ServiceTarefaImpl implements IServiceTarefa {

	/* Lista de tarefas */
	public List<Tarefa> listaTarefas;
	
	/* Objeto DAO */
	private ITarefaDAO dao;
	
	/* Construtor */
	public ServiceTarefaImpl() {
		dao = new TarefaDaoImpl();
		this.listaTarefas = new ArrayList<Tarefa>();
	}

	/* Cadastrar nova tarefa */
	@Override
	public String cadastrar(Tarefa tarefa) {
		try {
			UsuarioDaoImpl sdao = new UsuarioDaoImpl();
			tarefa.setResponsavel(sdao.findByPrimaryKey(tarefa.getResponsavel().getId()));
			dao.insert(tarefa);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/* Remover tarefa existente */
	@Override
	public String remover(Tarefa tarefa) {
		try {
			dao.remove(tarefa);
			listaTarefas.clear();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/* Editar tarefa existente */
	@Override
	public String editar(Tarefa tarefa) {
		
		try {
			dao.update(tarefa);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	public List<Tarefa> getTarefas(Sprint sprint) {
		
		try {
			listaTarefas = dao.findTarefaBySprint(sprint);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		
		return listaTarefas;
		
	}
	
	
	/* Retorna lista de tarefas */
	public List<Tarefa> getListaTarefas() {
		if (listaTarefas.isEmpty()) {
			try {
				listaTarefas = dao.findAll();
			}
			catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		return listaTarefas;
	}

	/* Preenche lista de tarefas */
	public void setListaTarefas(List<Tarefa> listaTarefas) {
		this.listaTarefas = listaTarefas;
	}

}
